<?php
/**
 * Feedback Model
 *
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD.
 * @license http://doit.yuandot.com/license/
 */

class Feedback extends CActiveRecord {
    /**
     * The followings are the available columns in table 'www_feedbacks':
     * @var integer $id
     * @var integer $node_id
     * @var string $username
     * @var integer $sex
     * @var string $tel
     * @var string $fax
     * @var string $mobile_phone
     * @var string $company_name
     * @var string $address
     * @var string $postcode
     * @var string $title
     * @var string $body
     * @var string $body_formatted
     * @var string $ip_address
     * @var integer $status
     * @var integer $is_replied
     * @var integer $created
     * @var integer $updated
     */

     public $status = Options::STATUS_DRAFT;

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{feedbacks}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('node_id, title, body', 'required'),
            array('node_id, sex, status, created, updated', 'numerical', 'integerOnly' => true),
            array('is_replied', 'boolean', 'trueValue' => 1, 'falseValue' => 0),
            array('username, tel, fax, mobile_phone', 'length', 'max' => 20),
            array('company_name', 'length', 'max' => 60),
            array('address, title', 'length', 'max' => 200),
            array('postcode', 'length', 'max' => 6),
            array('ip_address', 'length', 'max' => 15),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            // 所属栏目
            'node' => array(self::BELONGS_TO, 'Node', 'node_id'),
            // 留言回复
            'comment' => array(self::HAS_ONE, 'FeedbackComment', 'feedback_id'),
        );
    }

    public function behaviors() {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
                'updateAttribute' => 'updated',
            )
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'node_id' => Yii::t('common', 'node_id'),
            'username' => Yii::t('feedback', 'username'),
            'sex' => Yii::t('options', 'sex'),
            'tel' => Yii::t('feedback', 'tel'),
            'fax' => Yii::t('feedback', 'fax'),
            'mobile_phone' => Yii::t('feedback', 'mobile_phone'),
            'company_name' => Yii::t('feedback', 'company_name'),
            'address' => Yii::t('feedback', 'address'),
            'postcode' => Yii::t('feedback', 'postcode'),
            'title' => Yii::t('feedback', 'title'),
            'body' => Yii::t('feedback', 'body'),
            'body_formatted' => Yii::t('feedback', 'body'),
            'ip_address' => Yii::t('feedback', 'ip_address'),
            'status' => Yii::t('options', 'status'),
            'is_replied' => Yii::t('feedback', 'is_replied'),
            'created' => Yii::t('feedback', 'created'),
            'updated' => Yii::t('common', 'updated'),
        );
    }
    
    // "status" field Icon format
    public function getStatusIconFormat() {
        switch ($this->status) {
            case Options::STATUS_DRAFT:
                $img = 'status_draft.gif';
                break;
            case Options::STATUS_PUBLISHED:
                $img = 'status_published.gif';
                break;
            case Options::STATUS_PENDING_DELETE:
                $img = 'status_pending_delete.gif';
                break;
            default:
                $img = 'status_unknown.gif';
        }
        return CHtml::image(Yii::app()->assetManager->baseUrl . '/backend/images/' . $img);
    }

    // status AJax submit
    public function getStatusAjaxFormat() {
        return CHtml::ajaxLink($this->statusIconFormat,
            Yii::app()->createUrl('backend/feedbacks/changeStatus'),
            array(
                'type' => 'POST',
                'data' => 'id=' . $this->id,
                'dataType' => 'text',
                'update' => "#yuandot{$this->id}",
            ),
            array('id' => "yuandot{$this->id}")
        );
    }

    const REPLIED_NO = 0;
    const REPLIED_YES = 1;
    // "is_replied" field HTML format
    public function getRepliedHtmlFormat() {
        switch ($this->is_replied) {
            case self::REPLIED_NO:
                $icon = 'no.gif';
                break;
            case self::REPLIED_YES:
                $icon = 'yes.gif';
                break;
        }
        return CHtml::image(Yii::app()->assetManager->baseUrl
            . '/backend/images/' . $icon);
    }

    // "sex" field Text format
    public function getSexTextFormat() {
        $options = Options::sexOptions();
        return (isset($options[$this->sex])) ? $options[$this->sex] : 'unknown';
    }
    
    public function beforeValidate() {
        $this->ip_address = YUtil::getIp();
        $parser = new CMarkdownParser;
        $this->body_formatted = $parser->safeTransform($this->body);
        return true;
    }

    // "updated" field formatted
    public function getUpdatedFormatted() {
        return ($this->updated) ? date('Y-m-d', $this->updated) : '';
    }

    /**
     * update comment to this feedback.
     * This method will set status and post_id of the comment accordingly.
     * @param Comment the comment to be added
     * @return boolean whether the comment is saved successfully
     */
    public function addComment($comment) {
        $comment->feedback_id = $this->id;
        return $comment->save();
    }

    // 留言删除同时删除对应的回复内容
    protected function afterDelete() {
        $c = FeedbackComment::model()->find("feedback_id = {$this->id}");
        $c->delete();
    }
}